<?php

/*
 * 
 * TODO : list of items need to be done 
 *Shopping cart interface is for display of shopping cart that's available in session
 * 
 * find entire product line in database and show it 
 * add product line into an html table inside jquery dialog box?
 * add delete button 
 * add + button to increase qty 
 * add - button to decrease qty
 * 
 * add clear session button 
 * 
 * html tables should show : 
 *  product name 
    qty 
    price each 
    sub-total 
    delete button 
    tax
 *   
 * 
 * 
 * 
 */

//require_once '../Product/PRODUCT_session.php';
session_start();

$action = $_GET["Action"];
$ProductID= $_GET["ProductID"];
$quantity=$_GET["QTY"];


if ($action == "update")    
{
    updateQty($ProductID, $quantity);
}

function updateQty($id, $qty)
{
    require_once 'ShoppingCart.php';

    $result = isProductExist($id);
    if($result==false)
    {
        echo 'Record is not exist';
    }
    else 
    {
        $indexNumber = $result['productIndex'];
        $_SESSION['cart'][$indexNumber]['qty']=$qty;
        
    }
}

function ViewData()
{
$max = count($_SESSION['cart']);
$products = new SessionPRODUCT();
$total_row = array();
$findResult = array();
$json;

    for ($i=0 ; $i<$max;$i++)
    {
        //workaround for typecasting error which happens when explicitly used the PRODUCT object into array
        $json = json_encode($products->find($_SESSION['cart'][$i]['productid']));
        $findResult[]=  json_decode($json,TRUE);
        
    }
 
    
?>

<form name="input" action="../ShoppingCart/updateShoppingcart.php" target ="statusFrame" method="POST">

<table width="500" border="0">
  <tr>
    <th scope="col">Product Name</th>
    <th scope="col">qty</th>
    <th scope="col">Price each</th>
    <th scope="col">Row total</th>
    <th scope="col">Update</th>
    <th scope="col">Delete</th>
  </tr>

 <?  
  
  
    $max_result = count ($findResult);
     for ($y = 0; $y <$max_result; $y++)
     {
         echo '<tr>';
         echo '<td align = "center">'.$findResult[$y]['ProductName'].'</td>';
         //echo '<td align = "center"> '.$_SESSION['cart'][$y]['qty'].'</td>';
         echo '<td align = "center"> <input type="text" name="quantity[]" value = "'.$_SESSION['cart'][$y]['qty'].'"></td>';
         echo '<td align = "center">'.number_format($findResult[$y]['UnitPrice']).'</td>';
         echo '<td align = "center">'.number_format($_SESSION['cart'][$y]['qty']*$findResult[$y]['UnitPrice']).'</td>';
         //echo '<td align = "center"><input type="text" name="quantity[]"></td>';
         echo '</tr>';
         $total_row[]=$_SESSION['cart'][$y]['qty']*$findResult[$y]['UnitPrice'];
     }
  
     
 ?>

</table>
     <input type="submit" value="Update">
 </form>

<?php
}
?>